package com.zzhb;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.HistoryService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
public class PersonalTask {

	@Autowired
	RepositoryService repositoryService;

	@Autowired
	RuntimeService runtimeService;

	@Autowired
	TaskService taskService;

	@Autowired
	HistoryService historyService;

	@Test
	public void testDeploy() {
		Deployment deploy = repositoryService.createDeployment().name("personalTask")
				.addClasspathResource("processes/personalTask.bpmn").deploy();
		System.out.println(deploy.getId());
		System.out.println(deploy.getName());
	}

	// 流程启动
	@Test
	public void start() {
		String processDefinitionKey = "personalTask";
		String businessKey = "20181229";
		Map<String, Object> variables = new HashMap<>();
		variables.put("userId", "chenhj");
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey,
				variables);
		System.out.println(processInstance.getId());
		System.out.println(processInstance.getProcessDefinitionId());
	}

	// 查询个人所有的任务
	@Test
	public void testAssigneeTasks() {
		String assignee = "chenhj";
		List<Task> list = taskService.createTaskQuery().taskAssignee(assignee).list();
		for (Task task : list) {
			System.out.println("id=" + task.getId() + "=assignee=" + task.getAssignee() + "=name=" + task.getName()
					+ "=CreateTime=" + task.getCreateTime());
		}
	}

	// 完成任务
	@Test
	public void testComplete() {
		String taskId = "117505";
		Map<String, Object> vars = new HashMap<>();
		taskService.complete(taskId, vars);
		System.out.println("完成任务=" + taskId);
	}

}
